/*
 * com_jni_Cjj.cpp
 *
 *  Created on: 2018-12-27
 *      Author: zhangqing
 */
#include "com_jni_Cjj.h"
#include "string.h"
//char* to jstring
jstring stoJstring(JNIEnv* env, const char* pat)
{
	//定义java String类 strClass
	jclass strClass = (env)->FindClass("Ljava/lang/String;");
	//获取String(byte[],String)的构造器,用于将本地byte[]数组转换为一个新String
	jmethodID ctorID = (env)->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
	//建立byte数组
	jbyteArray bytes = (env)->NewByteArray(strlen(pat));
	//将char* 转换为byte数组
	(env)->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
	// 设置String, 保存语言类型,用于byte数组转换至String时的参数
	jstring encoding = (env)->NewStringUTF("UTF-8");
	//将byte数组转换为java String,并输出
	return (jstring)(env)->NewObject(strClass, ctorID, bytes, encoding);

}
JNIEXPORT jstring JNICALL Java_com_jni_Cjj_getComputername(JNIEnv *env, jobject) {
	char* a="123456";
	jstring result = stoJstring(env,"张青");
	return result;
}


